/*
 * File: tree.c
 * Author: Antonio Pessoa
 * Date: 18/05/2012
 * Description: The prime factors of 13195 are 5, 7, 13 and 29. What is the
 *              largest prime factor of the number 600851475143?
 */

#include <stdio.h>
#include <sys/unistd.h>

void tree() {
    int prime, div;
    unsigned long long int limit = 600851475143;
    
    for (div = 2; div * div < limit / 2; div++) {
        if (!(limit % div)) {
            if (checkprime(div)) {
                prime = div;
            }
        }
    }
    
    printf("\nPrime: %d\n\n", prime);
}

int checkprime(int num) {
    int cont = 0;
    
    for (int i = 2; i < num; i++) {
        if (!(num % i)) cont++;
    }
    
    return cont == 0 ? 1 : 0;
}
